package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     */
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {

        //判断当前加入购物车的商品是否已经存在
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);//对象拷贝
        Long userId = BaseContext.getCurrentId();//获取当前登录用户id
        shoppingCart.setUserId(userId);
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        //如果存在就将数量加1
        if (list != null && !list.isEmpty()) {
            ShoppingCart cart = list.get(0);
            cart.setNumber(cart.getNumber() + 1);//执行update语句进行更新 表示一次请求就添加一次数据
            shoppingCartMapper.updateNumberById(cart);//进行插入
        } else {
            //不存在就插入一条购物车信息
            Long dishId = shoppingCartDTO.getDishId();
            //判断本次添加到购物车的是菜品还是套餐
            if (dishId != null) {
                //本次添加的是菜品
                Dish dish = dishMapper.getByid(dishId);
                //给购物车对象进行赋值
                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());
            } else {
                //传递过来的是套餐
                Setmeal setmeal = setmealMapper.getById(shoppingCartDTO.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());
            }
            //下面代码属于公共部分，都是需要添加的 等全部填充完成后对数据库进行统一插入购物车数据
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /**
     * 查看购物车
     *
     * @param
     * @return
     */
    public List<ShoppingCart> showShoppingCart() {
        //获取当前微信登陆用户的id
        Long userId = BaseContext.getCurrentId();
        ShoppingCart cart = new ShoppingCart();
        cart.setUserId(userId);
        return shoppingCartMapper.list(cart);
    }

    /**
     * 清空购物车
     */
    public void clean() {
        //获取当前登录用户id
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.clean(userId);
    }

    /**
     * 删除购物车中的一个商品
     */
    public void cleanByNumber(ShoppingCartDTO shoppingCartDTO) {
        //传递过来的对象中数目减去1 当数目为0时直接清空该行数据
        //构建一个shoppingcart对象
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        modfiy(shoppingCart);
    }

    //抽取出菜品id或者套餐id的共同方法
    private void modfiy(ShoppingCart shoppingCart) {
        //根据传递过来的对象进行动态查询数据 获取唯一id
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        ShoppingCart cart = list.get(0);
        Integer beforeNumber = cart.getNumber();
        //如果当前用户的数量大于1就在原有的基础上减去1否则直接清除该行数据
        if (beforeNumber > 1) {
            cart.setNumber(beforeNumber - 1);
            //执行更新语句
            shoppingCartMapper.updateNumberById(cart);
        } else {
            cart.setNumber(0);
            //执行更新语句
            shoppingCartMapper.updateNumberById(cart);
            shoppingCartMapper.cleanByNumber(cart);
        }
    }
}
